package com.phamloc.shootingchicken.objects;

import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.phamloc.shootingchicken.extra.AudioPlayer;
import com.phamloc.shootingchicken.extra.AudioPlayer.MusicKeys;

public class ToggleButtonSprite extends ButtonSprite{
	
	private ToggleState mState = ToggleState.ON;
	private AudioPlayer mAudioPlayer;
	private OnToggleClickListener mOnToggleClickListener = null;
	private boolean mToogleState = true;
	public ToggleButtonSprite(float pX, float pY, ITextureRegion pTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager) {
        super(pX, pY,pTextureRegion ,pVertexBufferObjectManager);

	}
	public static enum ToggleState {
		ON(0), ON_PRESSED(1), OFF(2), OFF_PRESSED(3), DISABLE(4);
		private final int mState;
		private ToggleState(final int pSate) {mState = pSate;}
		public int get() {return mState;}
	};
	public interface OnToggleClickListener {
		void onOffClick(ToggleButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY);
		void onOnClick(ToggleButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY);
	};
	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		if (mState == ToggleState.DISABLE) {
			return true;
		}
		
		if (pSceneTouchEvent.isActionDown()) {
			setScale(0.5f);
			if (mState == ToggleState.ON) {
				setCurrentTileIndex(ToggleState.OFF_PRESSED.get());
				changeState(ToggleState.ON_PRESSED);
			} else if (mState == ToggleState.OFF) {
				setCurrentTileIndex(ToggleState.ON_PRESSED.get());
				changeState(ToggleState.OFF_PRESSED);
			}
		} else if (pSceneTouchEvent.isActionUp()) {

			if (mAudioPlayer != null) {
					mAudioPlayer.play(MusicKeys.GAME_MUSIC);
				 //mAudioPlayer.play(SoundKeys.BUTTON_PRESS);
			}
			if (mOnToggleClickListener != null) {
				if (mToogleState) {
					setCurrentTileIndex(ToggleState.OFF.get());
					changeState(ToggleState.ON);
					mOnToggleClickListener.onOnClick(this, pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
				} else {
					setCurrentTileIndex(ToggleState.ON.get());
					changeState(ToggleState.OFF);
					mOnToggleClickListener.onOffClick(this, pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
				}
				mToogleState = !mToogleState;
			}
			setScale(0.3f);
		}
		
		return true;
	}
	private void changeState(ToggleState onPressed) {
		mState = onPressed;
	}
	public ToggleState getmState() {
		return mState;
	}
	public void setState(ToggleState mState) {
		this.mState = mState;
	}
	public AudioPlayer getAudioPlayer() {
		return mAudioPlayer;
	}
	public void setAudioPlayer(AudioPlayer mAudioPlayer) {
		this.mAudioPlayer = mAudioPlayer;
	}
	public boolean isToogleState() {
		return mToogleState;
	}
	public void setToogleState(boolean mToogleState) {
		this.mToogleState = mToogleState;
	}
	
}
